var config = require('config');
cc.Class({
    extends: cc.Component,

    properties: {
        bgMusic: cc.AudioClip
      
    },
    
    // use this for initialization
    onLoad: function () {
        if(config.music_on){
            cc.audioEngine.playMusic(this.bgMusic, true);
        }
        this.action = cc.repeatForever(cc.blink(1, 1));
        this.node.runAction(this.action);
        cc.director.preloadScene('gamedraw', function () {
            cc.log('Next scene preloaded');
        });
        // this.getUserinfo(window.location.href);
    },
    
    nextScene: function () {
      cc.director.loadScene('gamedraw');
    },
    
    checkBgMusic: function(toggle){
        if(toggle.isChecked){
            config.music_on = true;
            cc.audioEngine.resumeMusic();
        }else{
            config.music_on = false;
            // cc.audioEngine.pause(this.audioId);
            cc.audioEngine.pauseMusic();
        }
    },
    
    getUserinfo: function(sHref){
      var args    = sHref.split("?");
      var retval = "";
      var userid = "";
      var username = "";
    
      if(args[0] == sHref) /*参数错误*/
      {
		  //alert('参数错误');
		  //window.close();
		  window.location.href="url....com/entry-login.html";
      }  
      var str = args[1];
      args = str.split("&");
      for(var i = 0; i < args.length; i ++)
      {
          str = args[i];
          var arg = str.split("=");
          if(arg.length <= 1) continue;
          if(arg[0] == 'userid'){
			  userid=arg[1];
			  config.userid = userid;
			  cc.log("保存数据uid");
		  }
		  if(arg[0] == 'name'){
		      username = arg[1];
		      config.username = username;
		  }
		  if(arg[0] == 'auth_code'){
		      var code = arg[1];
		      var xhr = new XMLHttpRequest();
              xhr.onreadystatechange = function () {
                  if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status < 400)) {
                    var response = xhr.responseText;
                    var res = JSON.parse(response);
                    console.log("res : " +response);
                    if (res['code'] === 0){
                        userid = res['userid'];
                        username = res['usename'];
                        config.userid = userid;
                        config.username = username;
                        cc.log("保存数据uid"+userid);
                    }else{
                        cc.log("auth_code 请重新登录");
                        window.location.href="url....com/entry-login.html";
                    }
                  }
                };
                // http://mysite/drawserver/login.php
                // url....com/draw/server/login.php
               xhr.open("GET", "url....com/draw/server/login.php?auth_code="+code, true);
               xhr.send();
               break;
		    }
		}

	  if (config.userid===""){
		  //alert('无法获取userid');
		  window.location.href="url....com/entry-login.html";
		  return;
	  }
    },

    // called every frame, uncomment this function to activate update callback
    // update: function (dt) {

    // },
});
